﻿package src.com.online.rpg
{
	/*
	  Enemy
	*/
	public class Enemy extends AnimatedGameObject
	{
		private var _startTimer:Number;
		
		private var _vx:Number;
		private var _vy:Number;
		
		public function Enemy() 
		{
			super( new Tbone );
			
			// random speed
			_vx = Math.random() * 100 + 50;
			_vy = Math.random() * 100 + 50;
			
			if (Math.random() > .5) _vx = -_vx;
			if (Math.random() > .5) _vy = -_vy;

			// wakeup timer, so player can't be instakilled
			_startTimer = 2;
		}
		
		public function get active():Boolean		{ return _startTimer <= 0; }
		
		public override function update(dt:Number):void
		{
			// tick wakeup timer
			if (_startTimer > 0)
			{
				_startTimer -= dt;
				graphics.visible = !graphics.visible;
				
				if (_startTimer <= 0)
				{
					_startTimer = 0;
					graphics.visible = true;
				}
				
				return;
			}
						
			super.update(dt);	// update animation
			
			// move only at specific parts of the animation
			if (checkAnimationFlag("moving"))
			{
				x += _vx*dt;
				y += _vy*dt;
			}
			
			// bounce off edges
			if (x < 0 || x > 400) _vx = -_vx;
			if (y < 0 || y > 400) _vy = -_vy;
			
			depth = y;
		}
		
		
	}

}